package base.thread_JUC2.chapter01.demo007;

import java.time.LocalDateTime;
import java.util.Deque;
import java.util.concurrent.TimeUnit;

/**
 * 事件写入队列任务（用户线程）
 * 创建Event，并写入队列，然后休眠1s，如此循环100次。
 *
 */
public class WriterTask implements Runnable{
    private Deque<Event> deque;

    public WriterTask(Deque<Event> deque) {
        this.deque = deque;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            Event event = new Event();
            event.setNow(LocalDateTime.now());
            System.out.println(Thread.currentThread().getName());
            event.setEvent(String.format("The thread %s has generated an event", Thread.currentThread().getId()));
            deque.addFirst(event);

            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
